//
//  HWyaoqingguizetankuanView.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/2/18.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWyaoqingguizetankuanView.h"

@interface HWyaoqingguizetankuanView()

@property(strong,nonatomic) UIView *grayView;
//@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) NSArray *icoArr;
@property(strong,nonatomic) NSArray *listArr;

@property(strong,nonatomic) UIImageView *fenxiangImge;


@property(strong,nonatomic)UILabel *label1;
@property(strong,nonatomic) UIButton *xiaohaoBut;

@property(strong,nonatomic) UILabel *yaoqingjiemianLabel;



@end


@implementation HWyaoqingguizetankuanView




- (instancetype)initWithFrame:(CGRect)frame  {
    
    if (self  = [super initWithFrame:frame]) {
        // 添加灰色背景
        [self addgrayView];
        
        [self setupUI];
        
        
    }
    return  self;
    
}

//  设置UI
- (void)setupUI {
    
    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(20, (kScreenH- 240)/2, kScreenW-40, 240)];
    self.fenxiangView.backgroundColor = RGB(255, 255, 255);
    self.fenxiangView.layer.cornerRadius = 12;
    self.fenxiangView.layer.masksToBounds = YES;
    [self addSubview:self.fenxiangView];
     
    
//    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-120)/2, 10, 120, 120)];
//    self.fenxiangImge = image1;
//    [self.fenxiangView addSubview:image1];
    
    
    UIView  *hunseView = [[UIView alloc]initWithFrame:CGRectMake( (kScreenW-40-146)/2, -10, 146, 74)];
    hunseView.layer.cornerRadius = 10l;
    hunseView.layer.masksToBounds = YES;
    hunseView.backgroundColor = RGB(250, 53, 0);
    
    hunseView.backgroundColor =  [UIColor  bm_colorGradientChangeWithSize:CGSizeMake(146, 74) direction:IHGradientChangeDirectionVertical startColor:RGB(255, 154, 119) endColor:RGB(250, 53, 0)];
    hunseView.hidden = YES;
    [ self.fenxiangView addSubview:hunseView ];
    
    
    
    
    
    
    UIImageView *imageqian1 = [[UIImageView alloc] initWithFrame:CGRectMake(27, 20, 93, 21)];
    imageqian1.image = [UIImage imageNamed:@"qiandaoyouli"];
    [hunseView addSubview:imageqian1];
    
    
    UILabel *sefenxianlabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, imageqian1.bottom+5, 146, 17)];
       sefenxianlabel1.text = @"连续签到获得更多好礼";
       sefenxianlabel1.font = [UIFont systemFontOfSize:12];
       sefenxianlabel1.textColor = RGB(255, 255, 255);
       sefenxianlabel1.textAlignment =  NSTextAlignmentCenter;
    [hunseView addSubview:sefenxianlabel1];
    
    
    
    
    
    
    
    
    UILabel *fenxianlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, kScreenW-40, 25)];
    fenxianlabel.text = @"活动规则";
    fenxianlabel.text =  [HWjianzhiduiObject keylocalization:@"活动规则"];
    fenxianlabel.font = [UIFont systemFontOfSize:18];
    fenxianlabel.textColor = RGB(68, 68, 68);
    fenxianlabel.textAlignment =  NSTextAlignmentCenter;
    self.label1 = fenxianlabel;
    [self.fenxiangView  addSubview:fenxianlabel];
    
    
    
    UILabel *fenxianlabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, fenxianlabel.bottom+1, kScreenW-40, 1)];
    fenxianlabel1.text = @"兑换说明";
    
    fenxianlabel1.text = [HWjianzhiduiObject keylocalization:@"兑换说明"];
    
    
     
    fenxianlabel1.font = [UIFont systemFontOfSize:14];
    fenxianlabel1.textColor = RGB(68, 68, 68);
    fenxianlabel1.textAlignment =  NSTextAlignmentCenter;
    fenxianlabel1.hidden = YES;
    [self.fenxiangView  addSubview:fenxianlabel1];
    
    
    
    
      UILabel *fenxianlabel3 = [[UILabel alloc]initWithFrame:CGRectMake(35, fenxianlabel1.bottom+2, kScreenW-70-40, 90)];
             fenxianlabel3.text = @"邀请好友注册账号后即可获得100积分，上不封顶。 \n好友在本APP充值平台币，您可以获得充值金额3%的平台币奖励，上不封顶。";
             fenxianlabel3.font = [UIFont systemFontOfSize:12];
             fenxianlabel3.textColor = RGB(136, 136, 136);
             fenxianlabel3.numberOfLines = 0;
             fenxianlabel3.textAlignment =  NSTextAlignmentLeft;
           fenxianlabel3.backgroundColor = [UIColor whiteColor];
    
    //   NSString *str = @"邀请好友注册账号后即可获得100积分，上不封顶。 \n好友在本APP充值平台币，您可以获得充值金额3%的平台币奖励，上不封顶。";
    
       self.yaoqingjiemianLabel = fenxianlabel3;
    
    
   //   [self setLineSpace:8.0f withText:str inLabel:fenxianlabel3];
            [self.fenxiangView  addSubview:fenxianlabel3];
    
    
    
   NSString *zhixiang = [HWjianzhiduiObject keylocalization:@"我已知晓"];
   
       UIButton *loginbutt  = [UIButton setButtonWithFrame:CGRectMake(20,fenxianlabel3.bottom+20,kScreenW-80, 44) Title:zhixiang Image:nil Font:15 Color:RGB(255, 255, 255) Target:self Selector:@selector(logtapTap:)];
    self.xiaohaoBut =loginbutt;
      //  loginbutt.backgroundColor = RGB(68, 94, 233);
      //  [loginbutt setBackgroundImage:[UIImage imageNamed:@"button_s"] forState:UIControlStateNormal];
    //    loginbutt.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
    //    loginbutt.layer.shadowOffset = CGSizeMake(0,0);
    //    loginbutt.layer.shadowOpacity = 1;
    //    loginbutt.layer.shadowRadius = 10;
    //    loginbutt.layer.cornerRadius = 6;
    //    loginbutt.layer.masksToBounds = YES;
    //    loginbutt.clipsToBounds = NO;
    
        loginbutt.backgroundColor = RGB(246, 106, 5);
        loginbutt.layer.masksToBounds = YES;
        loginbutt.layer.cornerRadius = 22;
    loginbutt.backgroundColor = [UIColor bm_colorGradientChangeWithSizeView:loginbutt];
    
        [self.fenxiangView addSubview:loginbutt];
    
    
//
//      UIButton *quxiaologinbutt  = [UIButton setButtonWithFrame:CGRectMake(20,loginbutt.bottom+5,kScreenW-80, 44) Title:@"取消" Image:nil Font:15 Color:RGB(136, 136, 136) Target:self Selector:@selector(quexiao:)];
//      //  loginbutt.backgroundColor = RGB(68, 94, 233);
//      //  [loginbutt setBackgroundImage:[UIImage imageNamed:@"button_s"] forState:UIControlStateNormal];
//    //    loginbutt.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
//    //    loginbutt.layer.shadowOffset = CGSizeMake(0,0);
//    //    loginbutt.layer.shadowOpacity = 1;
//    //    loginbutt.layer.shadowRadius = 10;
//    //    loginbutt.layer.cornerRadius = 6;
//    //    loginbutt.layer.masksToBounds = YES;
//    //    loginbutt.clipsToBounds = NO;
//
//        quxiaologinbutt.backgroundColor = RGB(255, 255, 255);
//        quxiaologinbutt.layer.masksToBounds = YES;
//        quxiaologinbutt.layer.cornerRadius = 22;
//        [self.fenxiangView addSubview:quxiaologinbutt];
//
//
//
    
    
    

    
    
    
    

//    self.listArr = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
//    self.icoArr = @[@"share_w",@"share_q",@"share_qqico",@"share_c"];
//    CGFloat jianju = (kScreenW - 82-4*50)/3;
//    // CGFloat  shujianju = (166-100)/3;
//
//    for (int i = 0 ; i < self.listArr.count; i++) {
//
//
//        UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(jianju*i+41+i*50, 52, 50, 50) Title:self.listArr[i] Image:self.icoArr[i] Font:13 Color:RGB(51, 51, 51) Target:self Selector:@selector(buttonTap:)];
//        butt.tag = i;
//        [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
//        // butt.backgroundColor = [UIColor redColor];
//        [self.fenxiangView addSubview:butt];
//
//    }
    
//    UIView *fengeview = [[UIView alloc]initWithFrame:CGRectMake(0,137, kScreenW, 1)];
//    fengeview.backgroundColor = RGB(215, 215, 215);
//
//
//    [self.fenxiangView addSubview:fengeview];
//
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame = CGRectMake(0, 138, kScreenW, 38);
//    [button setTitle:@"保存" forState:UIControlStateNormal];
//    [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(quiabuttontap) forControlEvents:UIControlEventTouchUpInside];
//    button.titleLabel.font = [UIFont systemFontOfSize:14];
//    [self.fenxiangView addSubview:button];
//
    
    
    
    
    
    
    
    
    
}


-(void)setDataDic:(NSDictionary *)dataDic {
    

   //  [self.xiaohaoBut setTitle:[NSString stringWithFormat:@"%@",jifenStr] forState:UIControlStateNormal];
    
    
    
}


- (void)relayoutTheChildControls {
//    // self.whiteView.backgroundColor = [UIColor redColor];
//    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
//    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
//

    
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.9;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    
    
    
    
}

- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}


//点击buton 按钮

- (void)buttonTap:(UIButton *)sender {
    
 //   [self.delegate xuazhongTag:(int)sender.tag];
    
     
        
    
   
    
}

#pragma mark ------ 取消
- (void)quiabuttontap {
    
  //  [self removeFromSuperview];
    
    
   UIImageWriteToSavedPhotosAlbum(self.fenxiangImge.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    #pragma mark -- <保存到相册>
   
    
    
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
        [MBProgressHUD showSuccess:@"保存失败"];
        
    }else{
        msg = @"保存图片成功" ;
           [MBProgressHUD showSuccess:@"保存成功"];
    }
}


-(void)setNoteStr:(NSString *)noteStr {
    
    _noteStr = noteStr;
    
    
    self.yaoqingjiemianLabel.text = noteStr;
    
    
    [self setLineSpace:8.0f withText:noteStr inLabel:self.yaoqingjiemianLabel];
      
    
}



//-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//
//        return NO;
//    }else if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableView"]) {
//
//
//        return NO;
//    }
//    return YES;
//
//
//}


//- (void)setUrlStr:(NSString *)urlStr {
//
//     _urlStr = urlStr;
//
//    self.fenxiangImge.image =[self createNonInterpolatedUIImageFormCIImage:[self creatQRcodeWithUrlstring:urlStr] withSize:150];
//
//
//
//
//
//}

- (CIImage *)creatQRcodeWithUrlstring:(NSString *)urlString{
    
    // 1.实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
    [filter setDefaults];
    // 3.将字符串转换成NSdata
    NSData *data  = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
    [filter setValue:data forKey:@"inputMessage"];
    // 5.生成二维码
    CIImage *outputImage = [filter outputImage];
    return outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}




- (void)logtapTap:(UIButton *)sender  {
    
    
    
//    HWRealNameAuthenticationController *authentication = [[HWRealNameAuthenticationController alloc]init];
//    [[HWViewUtils getCurrentVC].navigationController pushViewController:authentication animated:YES];
 //    [self.delegate  dianjiqiandao];
    
    
     
  [self removeFromSuperview];
    
    
        
     
    
}

- (void)quexiao:(UIButton *)sender  {
    
      [self removeFromSuperview];
    
}


-(void)setLineSpace:(CGFloat)lineSpace withText:(NSString *)text inLabel:(UILabel *)label{
    if (!text || !label) {
        return;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace;  //设置行间距
    paragraphStyle.lineBreakMode = label.lineBreakMode;
    paragraphStyle.alignment = label.textAlignment;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    label.attributedText = attributedString;
}
@end
